#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
// #include <bits/stdc++.h>
using namespace std;

//书库

struct TuShu {
    int id;
    string shuming;
    string zuozhe;
    int kucun;
	int jiechu;
};

vector<TuShu> tushu_list[10];

//用户库

struct YongHu{
	string i;
	string password;
};

YongHu yonghu[10];

//登陆状态

bool zt = false;

//总用户数

int yhs;

//当前账户地址

int dq;

//各馆馆存信息

int gc[10] = {0};

//欢迎界面

void HuanYing(){
	cout << "\n=== 欢迎使用多用户图书管理系统 ===\n";
	cout << "        当前版本：2.1.0\n";
	cout << "更新内容：	\n";
	cout << "	1.新增修改用户名功能\n";
	cout << "	2.修复了图书 ID 等数据输入非数字字符会死循环的问题\n";
	cout << "	3.修复了图书 ID 可能会重复的问题\n";
	cout << "	4.修复了部分bug\n";
	cout << "	5.新增了部分bug\n\n";
	cout << "产品特性：\n";
	cout << "	1.支持多用户功能（目前仅限 10 位用户且不可删改）\n";
	cout << "	2.支持图书借出和归还的功能\n";
	cout << "	3.首页提供馆存图书统计\n";
	cout << "	4.支持修改密码的功能\n";
	cout << "	5.支持人机验证的功能\n";
}

//人机验证

void RenJiYanZheng(){
	int a,b,c;
	a = rand() % 10;
	b = rand() % 10;
	cout << "\n==人机验证==\n";
	cout << "请输入 " << a << " + " << b << " = ";
	cin >> c;
	if(a + b != c) {
		cout << "\n人机验证失败，即将退出程序!\n";
		sleep(1);
		exit(0);
	}
}

//注册

void ZhuCe(){	
	if (yhs >= 10) {
		cout << "\n当前用户数已满！" << endl;
		return ;
	}
	while (1){
		YongHu xinyh;
		cout << "\n请输入新用户名：";
		cin >> xinyh.i;

		bool flag = false;

		for (int i = 0;i <= yhs;i++) {
			if (yonghu[i].i == xinyh.i) {
				cout << "\n该用户名已注册!\n";
				flag = true;
				break;
			}
		}

		if (flag) continue;

		string re;

		do{
			cout << "\n请输入密码：";
			cin >> xinyh.password;
			cout << "请再次输入密码：";
			cin >> re;
			if(re != xinyh.password) {
				cout << "\n两次密码不一致，请重新输入！\n\n";
			}
		}while(re != xinyh.password);

		cout << endl << xinyh.i << " 注册成功！\n";
		yonghu[yhs++] = xinyh;
		sleep(1);
		return ;
	}
}

//登陆

void DengLu(){
	if (yhs == 0) {
		cout << "\n暂无用户！\n";
		return ;
	}
	
	string target_i,target_pw;
	
	cout << "\n请输入用户名：";
	cin >> target_i;
	cout << "请输入密码：";
	cin >> target_pw;
	
	for (int i = 0;i <= yhs;i++) {
		if (yonghu[i].password == target_pw && yonghu[i].i == target_i) {
			dq = i;
			cout << "登录成功！\n\n您好 " << yonghu[i].i << "\n";
			zt = true;
			sleep(1);
			return;
		}
	}
	cout << "\n用户不存在或密码错误!\n";
}

//修改密码

void XiuGaiMiMa(){
	if (yhs == 0) {
		cout << "\n暂无用户！\n";
		return ;
	}
	string target_i,target_pw;
	cout << "\n请输入需要修改密码的用户名：";
	cin >> target_i;
	cout << "请输入原密码：";
	cin >> target_pw;
	for (int i = 0;i <= yhs;i++) {
		if (yonghu[i].password == target_pw && yonghu[i].i == target_i) {
			cout << "请输入新密码：";
			cin >> yonghu[i].password;
			cout << "\n修改成功\n";
			return;
		}
	}
	cout << "\n用户不存在或密码错误!\n";
}

//修改用户名

void XiuGaiYongHuMing(){
	if (yhs == 0) {
		cout << "\n暂无用户！\n";
		return ;
	}
	string target_i,target_pw;
	cout << "\n请输入需要修改的用户名：";
	cin >> target_i;
	cout << "请输入密码：";
	cin >> target_pw;
	for (int i = 0;i <= yhs;i++) {
		if (yonghu[i].password == target_pw && yonghu[i].i == target_i) {
			cout << "请输入新用户名：";
			cin >> yonghu[i].i;
			cout << "\n修改成功\n";
			return;
		}
	}
	cout << "\n用户不存在或密码错误!\n";
}

//登陆菜单

void DengLuCaiDan(int& choiceyh){
	cout << "\n=== 用户登陆 ===\n";
	cout << "1. 登陆\n";
	cout << "2. 注册\n";
	cout << "3. 修改密码\n";
	cout << "4. 修改用户名\n";
	cout << "0. 退出\n";
	cout << "请输入选项: ";
	
	cin >> choiceyh;
	
	switch(choiceyh) {
		case 1: DengLu(); break;
		case 2: ZhuCe(); break;
		case 3: XiuGaiMiMa();break;
		case 4: XiuGaiYongHuMing();break;
		case 0: cout << "\n感谢使用!\n";exit(0);
		default: cout << "\n无效输入!\n";
	}
}

//添加图书

void TianJiaTuShu() {
    TuShu new_book;
	string i;
	int num;
    cout << "\n输入图书ID: ";
    cin >> i;
	bool isdi;
	isdi = all_of(i.begin(), i.end(), ::isdigit);
	while(!isdi){
		cout << "图书ID应为数字，请重新输入！";
		cout << "\n输入图书ID: ";
		cin >> i;
		isdi = all_of(i.begin(), i.end(), ::isdigit);
	}

	for (const auto& book : tushu_list[dq]) {
		if (book.id == stoi(i)) {
			cout << "\n该图书ID已注册存在!\n";
			return ;
		}
	}

	new_book.id = stoi(i);
    cout << "输入书名: ";
	cin >> new_book.shuming;
    cout << "输入作者: ";
    cin >> new_book.zuozhe;
    cout << "输入库存量: ";
	string target_i;
	cin >> target_i;
    bool isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	while(!isdig){
		cout << "库存量应为数字，请重新输入！";
		cout << "\n输入库存量: ";
		cin >> target_i;
		isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	}
	new_book.kucun = stoi(target_i);
	gc[dq] += new_book.kucun;
	new_book.jiechu = 0;
    tushu_list[dq].push_back(new_book);
    cout << "\n添加成功!\n";
}

//显示列表

void XianShiLieBiao() {
	if (tushu_list[dq].empty()) {
		cout << "\n暂无图书信息！\n";
		return;
	}
	
    cout << "\n=== 图书列表 ===\n";
	for (const auto& book : tushu_list[dq]) {
		cout << "ID: " << book.id << endl;
		cout << "书名: " << book.shuming << endl;
		cout << "作者: " << book.zuozhe << endl;
		cout << "库存: " << book.kucun << endl;
		cout << "借出：" << book.jiechu << endl << endl;
	}
	sleep(1);
}

//删除图书

void ShanChuTuShu() {
	if (tushu_list[dq].empty()) {
		cout << "\n暂无图书信息！\n";
		return;
	}

	cout << "\n输入要删除的图书ID: ";
	string target_i;
	cin >> target_i;
	bool isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	while(!isdig){
		cout << "图书ID应为数字，请重新输入！";
		cout << "\n输入图书ID: ";
		cin >> target_i;
		isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	}
	const int target_id = stoi(target_i);
    
    for (auto it = tushu_list[dq].begin(); it != tushu_list[dq].end(); ++it) {
		int shanchuid = (*it).id;
        if (shanchuid == target_id) {
			gc[dq] -= (*it).kucun;
			gc[dq] += (*it).jiechu;
            tushu_list[dq].erase(it);
            cout << "\n删除成功!\n";
            return;
        }
    }
    cout << "\n未找到该ID的图书!\n";
}

//修改信息

void XiuGaiXinXi() {
	if (tushu_list[dq].empty()) {
		cout << "\n暂无图书信息！\n";
		return;
	}
	
    cout << "\n输入要修改的图书ID: ";
	string target_i;
	cin >> target_i;
	bool isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	while(!isdig){
		cout << "图书ID应为数字，请重新输入！";
		cout << "\n输入图书ID: ";
		cin >> target_i;
		isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	}
	const int target_id = stoi(target_i);
    
    for (auto& book : tushu_list[dq]) {
        if (book.id == target_id) {
            cout << "输入新书名: ";
            cin >> book.shuming;
            cout << "输入新作者: ";
            cin >> book.zuozhe;
			gc[dq] -= book.kucun;
            cout << "输入新库存: ";
        	string target_i;
        	cin >> target_i;
            bool isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
        	while(!isdig){
        		cout << "库存应为数字，请重新输入！";
        		cout << "\n输入新库存: ";
        		cin >> target_i;
        		isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
        	}
        	book.kucun = stoi(target_i);
			gc[dq] += book.kucun;	
			gc[dq] += book.jiechu;
			cout << "确认借出量(当前借出 " << book.jiechu << " 本)：";
        	string target_;
        	cin >> target_;
        	bool isdi = all_of(target_.begin(), target_.end(), ::isdigit);
        	while(!isdi) {
        		cout << "借出量应为数字，请重新输入！\n";
        		cout << "确认借出量(当前借出 " << book.jiechu << " 本)：";
        		cin >> target_;
        		isdi = all_of(target_.begin(), target_.end(), ::isdigit);
        	}
			int jie = stoi(target_);
			while(jie > book.kucun){
				cout << "数值不合法，请重新输入！\n";
				cout << "确认借出量(当前借出 " << book.jiechu << " 本)：";
				string target_;
				cin >> target_;
				bool isdi = all_of(target_.begin(), target_.end(), ::isdigit);
				while(!isdi) {
					cout << "借出量应为数字，请重新输入！\n";
					cout << "确认借出量(当前借出 " << book.jiechu << " 本)：";
					cin >> target_;
					isdi = all_of(target_.begin(), target_.end(), ::isdigit);
				}
				jie= stoi(target_);
			}
			book.jiechu = jie;
			gc[dq] -= book.jiechu;
            cout << "\n修改完成!\n";
            return;
        }
    }
    cout << "\n未找到该ID的图书!\n";
}

//查找图书

void ChaZhaoTuShu() {
	if (tushu_list[dq].empty()) {
		cout << "\n暂无图书信息！\n";
		return;
	}
	
    cout << "\n输入要查找的图书ID: ";
	string target_i;
	cin >> target_i;
	bool isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	while(!isdig){
		cout << "图书ID应为数字，请重新输入！";
		cout << "\n输入图书ID: ";
		cin >> target_i;
		isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	}
	const int target_id = stoi(target_i);
    
    for (const auto& book : tushu_list[dq]) {
        if (book.id == target_id) {
            cout << endl << "查询结果:" << endl;
            cout << "ID: " << book.id << endl;
            cout << "书名: " << book.shuming << endl;
            cout << "作者: " << book.zuozhe << endl;
            cout << "库存: " << book.kucun << endl;
			cout << "借出：" << book.jiechu << endl << endl;
            return;
        }
    }
    cout << "\n未找到该图书!\n";
}

//借出图书

void JieChuTuShu(){
	if (tushu_list[dq].empty()) {
		cout << "\n暂无图书信息！\n";
		return;
	}
	
	cout << "\n输入要借出的图书ID: ";
	string target_i;
	cin >> target_i;
	bool isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	while(!isdig){
		cout << "图书ID应为数字，请重新输入！";
		cout << "\n输入图书ID: ";
		cin >> target_i;
		isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	}
	const int target_id = stoi(target_i);

	for (auto& book : tushu_list[dq]) {
		if (book.id == target_id) {
			if(book.jiechu == book.kucun){
				cout << "\n当前已无库存！" << endl;
			}
			else{
				book.jiechu++;
				gc[dq]--;
				cout << "\n借出成功！" << endl;
			}
			return;
		}
	}
	cout << "\n未找到该ID的图书！\n";
}

//归还图书

void GuiHuanTuShu(){
	if (tushu_list[dq].empty()) {
		cout << "\n暂无图书信息！\n";
		return;
	}
	
	cout << "\n输入要归还的图书ID: ";
	string target_i;
	cin >> target_i;
	bool isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	while(!isdig){
		cout << "图书ID应为数字，请重新输入！";
		cout << "\n输入要归还的图书ID: ";
		cin >> target_i;
		isdig = all_of(target_i.begin(), target_i.end(), ::isdigit);
	}
	const int target_id = stoi(target_i);

	for (auto& book : tushu_list[dq]) {
		if (book.id == target_id) {
			if(book.jiechu <= 0){
				cout << "\n当前暂无借出，请确认是否为我馆图书！" << endl;
			}
			else{
				book.jiechu--;
				gc[dq]++;
				cout << "\n归还成功！" << endl;
			}
			return;
		}
	}
	cout << "\n未找到该ID的图书！\n";
}

//主菜单

void ZhuCaiDan(int& choice){
	do {
		
		cout << "\n=== 您好！" << yonghu[dq].i << " ===\n";
		cout << "当前共有 " << tushu_list[dq].size() << " 种图书" << endl;
		cout << "当前馆存 " << gc[dq] << " 本图书" << endl;
		cout << "1. 添加图书\n";
		cout << "2. 显示列表\n";
		cout << "3. 删除图书\n";
		cout << "4. 修改信息\n";
		cout << "5. 查找图书\n";
		cout << "6. 借出图书\n";
		cout << "7. 归还图书\n";
		cout << "0. 退出登陆\n";
		cout << "请输入选项: ";
		
		cin >> choice;
		
		switch(choice) {
			case 1: TianJiaTuShu(); break;
			case 2: XianShiLieBiao(); break;
			case 3: ShanChuTuShu(); break;
			case 4: XiuGaiXinXi(); break;
			case 5: ChaZhaoTuShu(); break;
			case 6: JieChuTuShu();break;
			case 7:	GuiHuanTuShu();break;
			case 0: cout << "\n您已退出登录！\n" << "\n 再见 " << yonghu[dq].i << endl; zt = false;dq = -1;sleep(1);break;
			default: cout << "无效输入!\n";
		}
		sleep(1);
	} while (choice != 0);
}


int main() {
	
	/*made by exm-zem
	CSDN:https://blog.csdn.net/zemexm?spm=1018.2118.3001.5148
	gitee:https://gitee.com/exm-zem/freshman-internal-competition-plan*/
	
	srand(time(0));

	HuanYing();
	
	sleep(1); 
	
	RenJiYanZheng();
	
	int choiceyh,choice;
	do {
		
		DengLuCaiDan(choiceyh);
		
		if (zt == false) {
			continue;
		}
	
		ZhuCaiDan(choice);
	    
	} while (choiceyh != 0);
	
    return 0;
}
